from app import app
import os,requests,json,re
import MySQLdb,os,sys,csv
import MySQLdb.cursors
@app.route('/')
def hello():
    r=requests.get('http://www.hfjy.com')
    code=r.status_code
    return code

@app.route('/login')
def login():

    #s = requests.Session()
    #s.headers={'Accept-Encoding': 'gzip','transfer-encoding': 'chunked','connection': 'keep-alive','content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    params = {'email': 'xiaofei.lu@foxmail.com', 'password': '111111','remember':'check'}
    headers={'content-type': 'application/json','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36','X-Requested-With':'XMLHttpRequest','X-CSRF-Token':'gkn6QWUz-v2IQD6cCz3horj_U6SD7L4A19Ck'}
    r=requests.get('https://shimo.im/login',headers=headers,verify=False)
    b=re.findall("\'X-CSRF-Token.*\'",r.content)
    return b[0][17:-1]

@app.route('/dologin')
def dologin():

    #s = requests.Session()
    #s.headers={'Accept-Encoding': 'gzip','transfer-encoding': 'chunked','connection': 'keep-alive','content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}

    #
    params = {'email': 'xiaofei.lu@foxmail.com', 'password': '111111','remember':'check'}
    headers={'content-type': 'application/json','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36','X-Requested-With':'XMLHttpRequest','X-CSRF-Token':'gkn6QWUz-v2IQD6cCz3horj_U6SD7L4A19Ck'}
    r=requests.get('https://shimo.im/login',headers=headers,verify=False)
    b=re.findall("\'X-CSRF-Token.*\'",r.content)
    '''
    headers=r.cookies['_csrf']

    cook={};
    cook['_csrf']=r.cookies['_csrf']
    cook['gr_user_id']=r.cookies['gr_user_id']
    cook['rememberMeToken']=r.cookies['rememberMeToken']
    cook['ignore_this_cookies']=r.cookies['ignore_this_cookies']
    cook['userId']=r.cookies['userId']
    cook['shimo_sid']=r.cookies['shimo_sid']
    '''
    #return str(headers);
    token=b[0][17:-1]

    params = {'email': 'mengxiaoxiao@hfjy.com', 'password': 'xie..1017','remember':'check'}
    #params = {'email': 'xiaofei.lu@foxmail.com', 'password': '111111','remember':'check','geetest_challenge':'4cf0c8c47f648f295f02e139288c9402l1','geetest_validate':'ca053c365a4a035194d1b40a3f22ed5c','geetest_seccode':'ca053c365a4a035194d1b40a3f22ed5c|jordan'}
    headers={'Accept': 'application/json,text/javascript','Content-Type':'application/x-www-form-urlencoded;charset=UTF-8','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36','X-Requested-With':'XMLHttpRequest','Host':'shimo.im','X-CSRF-Token':token}
    r=requests.post('https://shimo.im/login',data=params,headers=headers,cookies=r.cookies,verify=False)
    #return str(r.url)
    return str(r.content)

@app.route('/test')
def test():

    #s = requests.Session()
    #s.headers={'Accept-Encoding': 'gzip','transfer-encoding': 'chunked','connection': 'keep-alive','content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    params = {'mobile': '15121045009', 'password': '111111'}
    headers={'content-type': 'application/json','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36','X-Requested-With':'XMLHttpRequest'}
    r=requests.post('https://shop-test.hfjy.com/user.php?act=login',data=params,verify=False)
    return r.content
    return eval(str(r.cookies))


@app.route('/curl')
def curl():

    #s = requests.Session()
    #s.headers={'Accept-Encoding': 'gzip','transfer-encoding': 'chunked','connection': 'keep-alive','content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}
    params = {'phone': '15600008888', 'password': '25d55ad283aa400af464c76d713c07ad','userType':0,'isAutoLogin':1}
    headers={'content-type': 'application/json','Content-Type':'application/x-www-form-urlencoded; charset=UTF-8','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36','X-Requested-With':'XMLHttpRequest'}
    r=requests.post('https://xue-dev.hfjy.com/study/common/User/login',data=params,verify=False)
    return str(r.status_code)+str(r.headers)

@app.route('/sql')
def sql():

    # coding=gbk
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    path=os.getcwd()+'/'
    origin_file=path+'course.csv'
    input_file={'error':path+'error.txt','right':path+'right.txt'}
    course_file=open(origin_file, 'rb')

    reader=csv.reader(course_file)
    rows=[];
    for row in reader:
        rows.append(row)
    course_file.close()


    conn = MySQLdb.connect("54json.com","slave","slave","student",charset='utf8',cursorclass = MySQLdb.cursors.DictCursor)

    cursor = conn.cursor()

    for row in rows:
        tt=row[0].decode('gbk')
        if tt:
            cursor.execute ("select * from student where name='"+tt+"' limit 4")
            count=cursor.rowcount
            count=int(count)

            if count==1:
                pass
                data=cursor.fetchone()
                print tt+'--'+str(count)
                print_out(data)
            elif count>=2:
                pass
                datas=cursor.fetchall()
                print tt+'--'+str(count)
                for data in datas:
                    print_out(data)
    cursor.close ()
    conn.close ()
    return 'ok'

    res = cursor.fetchone()
    path=os.getcwd()+'/txt.txt'

    f=open(path,'a')
    for row in res:
        #f.write('no')
        print(unicode(row))
    f.close()
    cursor.close ()
    conn.close ()


    return 'ok'


'''
conn = MySQLdb.connect("54json.com","slave","slave","student",charset='utf8')

cursor = conn.cursor()
cursor.execute ("select * from student limit 4")

res = cursor.fetchall()


f=open(path,'a')
for row in res:
    f.write('ee')
    print(unicode(row))
f.close()
cursor.close ()
conn.close ()
'''
def print_out(data):
    temp_str=str(data['student_intention_id'])+'--'+str(data['student_id'])+'--'+str(data['phone'])+'--'+data['name']+'--'+str(data['remaining_time'])
    print temp_str
    temp_str=str(data['student_intention_id'])+'--'+str(data['student_id'])+'--'+str(data['phone'])+'--'+data['name']+'--'+str(data['remaining_time'])+"\r\n"
    write_file(temp_str)

def write_file(str):
    f=open(os.getcwd()+'/right.txt','a',)
    f.write(str)
    f.close()